import {message} from 'antd'

import * as api from '../services/warehouse'

export default {
  namespace: 'warehouse',

  state: {
    list: [],
    total: 0,
  },

  effects: {
    *list(_, {call, put}) {
      const {data} = yield call(api.list, {page: 1, per_page: 1000}) // 仓库列表
      yield put({
        type: 'saveList',
        payload: data,
      })
    },
    *remove({payload}, {call, put}) {
      yield call(api.remove, payload)
      yield put({
        type: 'saveRemove',
        payload,
      })
      message.success('删除成功')
    },
    *update({payload, callback}, {call}) {
      yield call(api.update, payload)
      callback()
      message.success('更新成功')
    },
    *add({payload, callback}, {call}) {
      yield call(api.add, payload)
      callback()
      message.success('添加成功')
    }
  },

  reducers: {
    saveList(state, {payload}) {
      return {
        ...state,
        ...payload,
      }
    },
    saveRemove(state, {payload}) {
      const list = state.list.filter(item => item.id !== payload)
      return {...state, list}
    }
  },
}
